package v1

import (
	"ginblog/api/common"
	"ginblog/model"
	"ginblog/tools/pagination"
	"ginblog/utils/errmsg"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
)

// ArticleCategoryList /文章分类
func ArticleCategoryList(c *gin.Context) {
	category := model.Category{}
	_ = c.ShouldBindBodyWith(&category, binding.JSON)

	page := pagination.Pagination{}
	_ = c.ShouldBindBodyWith(&page, binding.JSON)

	category.SelectAllCat(&page)
	if category.GetCode() != 0 {
		common.FailJson(c, category.GetCode())
		return
	}
	common.SuccessJson(c, errmsg.SUCCESS, page)
}

// AddArticleCategory /添加文章分类
func AddArticleCategory(c *gin.Context) {
	category := model.Category{}
	_ = c.ShouldBindBodyWith(&category, binding.JSON)
	if category.CategoryName == "" {
		common.FailJson(c, errmsg.ARTICLE_NAME_IS_NULL)
		return
	}
	data := category.AddCat()
	if category.GetCode() != 0 {
		common.FailJson(c, category.GetCode())
		return
	}
	common.SuccessJson(c, errmsg.SUCCESS, data)
}

// EditArticleCategory /编辑文章分类
func EditArticleCategory(c *gin.Context) {
	category := model.Category{}
	_ = c.ShouldBindBodyWith(&category, binding.JSON)
	if category.CategoryName == "" {
		common.FailJson(c, errmsg.ARTICLE_NAME_IS_NULL)
		return
	}
	if !category.EditCat() {
		common.FailJson(c, errmsg.ARTICLE_EDIT_FAIL)
		return
	}
	common.SuccessJson(c, errmsg.SUCCESS, []interface{}{})
}

// DelArticleCategory /删除文章分类
func DelArticleCategory(c *gin.Context) {
	category := model.Category{}
	_ = c.ShouldBindBodyWith(&category, binding.JSON)
	if category.ID == 0 {
		common.FailJson(c, errmsg.DELETE_FAIL)
		return
	}
	if !category.DelCat() {
		common.FailJson(c, errmsg.DELETE_FAIL)
		return
	}
	common.SuccessJson(c, errmsg.SUCCESS, []interface{}{})
}

// ArticleList /文章列表
func ArticleList(c *gin.Context) {
	article := model.Article{}
	_ = c.ShouldBindBodyWith(&article, binding.JSON)

	page := pagination.Pagination{}

	_ = c.ShouldBindBodyWith(&page, binding.JSON)

	article.SelectAllArt(&page)
	if article.GetCode() != 0 {
		common.FailJson(c, article.GetCode())
		return
	}
	common.SuccessJson(c, errmsg.SUCCESS, page)
}

// ArticleCatList /查询对应分类下的所有文章
func ArticleCatList(c *gin.Context) {
	article := model.Article{}
	_ = c.ShouldBindBodyWith(&article, binding.JSON)

	page := pagination.Pagination{}

	_ = c.ShouldBindBodyWith(&page, binding.JSON)

	article.SelectCatArt(&page)
	if article.GetCode() != 0 {
		common.FailJson(c, article.GetCode())
		return
	}
	common.SuccessJson(c, errmsg.SUCCESS, page)
}

// ArticleListRec /推荐文章列表
func ArticleListRec(c *gin.Context) {
	article := model.Article{}
	_ = c.ShouldBindBodyWith(&article, binding.JSON)

	page := pagination.Pagination{}

	_ = c.ShouldBindBodyWith(&page, binding.JSON)

	article.SelectAllArtRec(&page)
	if article.GetCode() != 0 {
		common.FailJson(c, article.GetCode())
		return
	}
	common.SuccessJson(c, errmsg.SUCCESS, page)
}

// AddArticle /添加文章
func AddArticle(c *gin.Context) {
	article := model.Article{}
	_ = c.ShouldBindBodyWith(&article, binding.JSON)
	if article.ArticleName == "" {
		common.FailJson(c, errmsg.ARTICE_NAME_IS_NULL)
		return
	}
	data := article.AddArt()
	if article.GetCode() != 0 {
		common.FailJson(c, article.GetCode())
		return
	}
	common.SuccessJson(c, errmsg.SUCCESS, data)
}

// EditArticle /编辑文章
func EditArticle(c *gin.Context) {
	article := model.Article{}
	_ = c.ShouldBindBodyWith(&article, binding.JSON)
	if !article.EditArt() {
		common.FailJson(c, errmsg.ARTICE_EDIT_FAIL)
		return
	}
	common.SuccessJson(c, errmsg.SUCCESS, []interface{}{})
}

// DelArticle /删除文章
func DelArticle(c *gin.Context) {
	article := model.Article{}
	_ = c.ShouldBindBodyWith(&article, binding.JSON)
	if article.ID == 0 {
		common.FailJson(c, errmsg.DELETE_FAIL)
		return
	}
	if !article.DelArt() {
		common.FailJson(c, errmsg.DELETE_FAIL)
		return
	}
	common.SuccessJson(c, errmsg.SUCCESS, []interface{}{})
}

// TagList /分类列表
func TagList(c *gin.Context) {
	tag := model.Tag{}
	_ = c.ShouldBindBodyWith(&tag, binding.JSON)

	page := pagination.Pagination{}

	_ = c.ShouldBindBodyWith(&page, binding.JSON)

	tag.SelectAllTag(&page)
	if tag.GetCode() != 0 {
		common.FailJson(c, tag.GetCode())
		return
	}
	common.SuccessJson(c, errmsg.SUCCESS, page)
}

// AddTag /添加分类
func AddTag(c *gin.Context) {
	tag := model.Tag{}
	_ = c.ShouldBindBodyWith(&tag, binding.JSON)
	if tag.TagName == "" {
		common.FailJson(c, errmsg.TAG_NAME_IS_NULL)
		return
	}
	data := tag.AddTag()
	if tag.GetCode() != 0 {
		common.FailJson(c, tag.GetCode())
		return
	}
	common.SuccessJson(c, errmsg.SUCCESS, data)
}

// EditTag /编辑分类
func EditTag(c *gin.Context) {
	tag := model.Tag{}
	_ = c.ShouldBindBodyWith(&tag, binding.JSON)
	if !tag.EditTag() {
		common.FailJson(c, errmsg.TAG_EDIT_FAIL)
		return
	}
	common.SuccessJson(c, errmsg.SUCCESS, []interface{}{})
}

// DelTag /删除分类
func DelTag(c *gin.Context) {
	tag := model.Tag{}
	_ = c.ShouldBindBodyWith(&tag, binding.JSON)
	if tag.ID == 0 {
		common.FailJson(c, errmsg.DELETE_FAIL)
		return
	}
	if !tag.DelTag() {
		common.FailJson(c, errmsg.DELETE_FAIL)
		return
	}
	common.SuccessJson(c, errmsg.SUCCESS, []interface{}{})
}
